package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.FeedBiz;
import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.constant.FeedIdType;
import cn.sumpu.app.guoguojie.constant.FeedType;
import cn.sumpu.app.guoguojie.constant.FriendStatus;
import cn.sumpu.app.guoguojie.constant.GroupClassify;
import cn.sumpu.app.guoguojie.constant.MessageType;
import cn.sumpu.app.guoguojie.dao.FriendDao;
import cn.sumpu.app.guoguojie.dao.MessageDao;
import cn.sumpu.app.guoguojie.domain.Feed;
import cn.sumpu.app.guoguojie.domain.Friend;
import cn.sumpu.app.guoguojie.domain.FriendGroup;
import cn.sumpu.app.guoguojie.domain.Group;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.Ext.FriendFamily;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class FriendBizImpl implements FriendBiz {

	private FriendDao friendDao;
	private MessageDao messageDao;
	@Autowired
	private FeedBiz feedBiz;
	
	
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setFriendDao(FriendDao friendDao) {
		this.friendDao = friendDao;
	}

	@Override
	public FriendFamily searchFriendEmail(String myFamily,String email) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myFamily", myFamily);
		map.put("email", email);
		return friendDao.searchFriendEmail(map);
	}


	@Override
	public FriendFamily searchFriendDoorplate(String myFamily,String doorplate)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myFamily", myFamily);
		map.put("doorplate", doorplate);
		return friendDao.searchFriendDoorplate(map);
	}
	
	//根据标签名称查找好友
	@Override
	public PageModel searchFriendByTag(String tagName,String doorplate, Integer page)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", doorplate);
		map.put("tagName", tagName);
		
		Integer startId = (page - 1) * Common.PAGE_SIZE_FRIEND;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_FRIEND);
		List<FriendFamily> fList = friendDao.searchFriendByTag(map);
		int count = friendDao.searchFriendByTagCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(fList);
		pm.setPageSize(Common.PAGE_SIZE_FRIEND);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_FRIEND - 1) / Common.PAGE_SIZE_FRIEND);
		return pm;
	}


	@Override
	public List<FriendFamily> searchFriendCondition(String hostName, String fnickName,
			String sex, String birthday, String birthPro, String birthCity,
			String birthDic, String residePro, String resideCity,
			String resideDic, String babyName) throws AppBusinessException{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("hostName", hostName);
		map.put("fnickName", fnickName);
		map.put("sex", sex);
		map.put("birthday", birthday);
		map.put("birthPro", birthPro);
		map.put("birthCity", birthCity);
		map.put("birthDic", birthDic);
		map.put("residePro", residePro);
		map.put("resideCity", resideCity);
		map.put("resideDic", resideDic);
		map.put("babyName", babyName);
		return friendDao.searchFriendCondition(map);
	}

	
	@Override
	public PageModel searchFriendCondition(String myFamily,String username, String fnickName,
			String sex, String birthday, String birthPro, String birthCity,
			String birthDic, String province, String city,
			String district, String babyName,Integer page) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myFamily", myFamily);
		map.put("username", username);
		map.put("fnickName", fnickName);
		map.put("sex", sex);
		map.put("birthday", birthday);
		map.put("birthPro", birthPro);
		map.put("birthCity", birthCity);
		map.put("birthDic", birthDic);
		map.put("province", province);
		map.put("city", city);
		map.put("district", district);
		map.put("babyName", babyName);
		
		Integer startId = (page - 1) * Common.PAGE_SIZE_FRIEND;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_FRIEND);
		List<FriendFamily> fList = friendDao.searchFriendCondition(map);
		int count = friendDao.searchFriendConditionCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(fList);
		pm.setPageSize(Common.PAGE_SIZE_FRIEND);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_FRIEND - 1) / Common.PAGE_SIZE_FRIEND);
		return pm;
	}
	
	
	@Override
	public PageModel maybeFriends(String myFamily,Integer page) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myFamily", myFamily);
		
		Integer startId = (page - 1) * Common.PAGE_SIZE_FRIEND;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_FRIEND);
		List<FriendFamily> fList = friendDao.maybeFriends(map);
		int count = friendDao.maybeFriendsCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(fList);
		pm.setPageSize(Common.PAGE_SIZE_FRIEND);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_FRIEND - 1) / Common.PAGE_SIZE_FRIEND);
		return pm;
	}

	
	@Override
	public List<FriendFamily> maybeFriendsMain(String myFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myFamily", myFamily);
		map.put("orderby", "c.gmt_created");
		Integer startId = 0;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_MAYBE_FRIEND_MAIN);
		List<FriendFamily> fList = friendDao.maybeFriends(map);
		return fList;
	}

	/**
	 * 邀请好友
	 *  (non-Javadoc)
	 * @see cn.sumpu.app.guoguojie.biz.FriendBiz#inviteFriend(cn.sumpu.app.guoguojie.domain.Friend)
	 */
	@Override
	public void inviteFriend(Friend friend) throws AppBusinessException {
		friend.setIdentifier(IdGenerator.createIdentifier());
		friend.setStatus(FriendStatus.INVITE.getValue());
		friendDao.insertFriend(friend);
	}

	
	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void agreeFriend(Member member, String friend,String msgId)throws AppBusinessException {
		messageDao.deleteMessageById(msgId);
		if(isExsitFriend(friend, member.getFamily(), FriendStatus.ISFRIEND.getValue())){
			return;
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", friend);
		map.put("friend", member.getFamily());
		List<Friend> existFriend = friendDao.getFriend(map);
//		同意好友状态
		map.put("status", FriendStatus.ISFRIEND.getValue());
		friendDao.agreeFriend(map);
		if(existFriend!=null && existFriend.size() > 0){
			setGroupFriendDef(friend, member.getFamily(),existFriend.get(0).getIdentifier());
		}
		
//		如果好友已经存在则将状态变为是好友否则插入是好友的状态
		if(isExsitFriend(member.getFamily(), friend)){
			Map<String,Object> map2 = new HashMap<String,Object>();
			map2.put("family", member.getFamily());
			map2.put("friend", friend);
//			同意好友状态
			map.put("status", FriendStatus.ISFRIEND.getValue());
			friendDao.agreeFriend(map);
		}else{
			Friend f = new Friend();
			String friendId = IdGenerator.createIdentifier();
			f.setIdentifier(friendId);
			f.setFriend(friend);
			f.setFamily(member.getFamily());
			f.setStatus(FriendStatus.ISFRIEND.getValue());
			friendDao.insertFriend(f);
			
			setGroupFriendDef(member.getFamily(), friend,friendId);
			
			Feed feed = new Feed();
			feed.setMember(member.getIdentifier());
			feed.setFeedType(FeedType.Friend.getValue());
			feed.setFeedId(friend);
			feed.setFeedIdType(FeedIdType.FAMILYID.getValue());
			feed.setFamily(member.getFamily());
//			第一次同意插入动态
			feedBiz.insert(feed);
		}
		
		
		
	}
	
	/**
	 * 将好友放入默认分组
	 * @param family
	 * @param friend
	 * @throws AppBusinessException
	 */
	public void setGroupFriendDef(String family,String friend,String friendId) throws AppBusinessException{
//		将好友默认到我的好友分组中
		Map<String,Object> groupMap = new HashMap<String,Object>();
		groupMap.put("family", family);
		groupMap.put("classify", GroupClassify.FRIEND.getValue());
		List<Group> groupList = friendDao.getGroupList(groupMap);
		FriendGroup friendGroup = new FriendGroup();
		friendGroup.setFriend(friendId);
		if(groupList!=null && groupList.size() > 0){
			Group group = groupList.get(0);
			friendGroup.setGroup(group.getIdentifier());
		}else{
			Group g = createGroup(family, "朋友", GroupClassify.FRIEND.getValue(),null);
			friendGroup.setGroup(g.getIdentifier());
		}
		friendDao.insertFriendGroup(friendGroup);
	}
	
	

	@Override
	public void ignoreFriend(Member member, String friend, String msgId)
			throws AppBusinessException {
		messageDao.deleteMessageById(msgId);
	}

	@Override
	public Group createGroup(String family, String name,String classify, String remark)
			throws AppBusinessException {
		Group group = new Group();
		group.setIdentifier(IdGenerator.createIdentifier());
		group.setFamily(family);
		group.setName(name);
		group.setClassify(classify);
		group.setRemark(remark);
		friendDao.insertGroup(group);
		return group;
	}

	@Override
	public List<Group> getGroupList(String family) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		return friendDao.getGroupList(map);
	}
	



	

	

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void deleteFriend(String friend, String family)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("friend", friend);
		map.put("family", family);
//		删除好友的同时删除好友分组		
		friendDao.deleteFriendGroup(map);
		
		friendDao.deleteFriend(map);
		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("friend", family);
		map2.put("family", friend);
		friendDao.deleteFriend(map2);
		

	}

	@Override
	public PageModel getMyFriendsFamily(String family,String group,Integer page)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		map.put("group", group);
		
		Integer startId = (page - 1) * Common.PAGE_SIZE_FRIEND;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_FRIEND);
		List<FriendFamily> fList = friendDao.getMyFriendsFamily(map);
		int count = friendDao.getMyFriendsFamilyCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(fList);
		pm.setPageSize(Common.PAGE_SIZE_FRIEND);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_FRIEND - 1) / Common.PAGE_SIZE_FRIEND);
		return pm;
	}
	@Override
	public List<FriendFamily> getMyFriendsFamilySize(String family,Integer size) throws AppBusinessException{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		
		map.put("startId", 0);
		map.put("pageSize", size);
		return friendDao.getMyFriendsFamily(map);
	}

	@Override
	public void updateFriendGroup(String friendId, String group)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("friend", friendId);
		map.put("group", group);
		friendDao.updateFriendGroup(map);
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void friendAddSave(Member member, String family, String content)
			throws AppBusinessException {
		Friend friend = new Friend();
		friend.setIdentifier(IdGenerator.createIdentifier());
//		添加状态
		friend.setStatus(FriendStatus.INVITE.getValue());
//		好友家的门牌号
		friend.setFriend(family);
//		我家的门牌号
		friend.setFamily(member.getFamily());
		
		if(!isExsitFriend(member.getFamily(), family)){
			friendDao.insertFriend(friend);
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		Message m = new Message();
		m.setSendMember(member.getIdentifier());
		m.setReceiveFamily(family);
		m.setType(MessageType.FRIEND.getValue());
		m.setContent(content);
		m.setIdentifier(IdGenerator.createIdentifier());
		m.setTitle("添加好友！");
		m.setIsRead(false);
		messageDao.insertMessage(m);
	}
	
	/**
	 * 好友是否已经存在
	 * @param family
	 * @param friend
	 * @return
	 * @throws AppBusinessException
	 */
	public boolean isExsitFriend(String family,String friend) throws AppBusinessException{
		return isExsitFriend(family, friend, null);
	}
	
	/**
	 * 好友是否已经存在
	 * @param family
	 * @param friend
	 * @return
	 * @throws AppBusinessException
	 */
	public boolean isExsitFriend(String family,String friend,String status) throws AppBusinessException{
		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("family", family);
		map2.put("friend", friend);
		map2.put("status", status);
		List<Friend> friendList = friendDao.getFriend(map2);
		return (friendList!=null &&friendList.size() > 0);
	}

	@Override
	public Group getGroupDefault(String family) throws AppBusinessException {
		getGroupList(family);
		return null;
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public Group addFriendGroup(String family, String groupName,String friendId)
			throws AppBusinessException {
		Group group = createGroup(family, groupName, GroupClassify.CUSTOM.getValue(), null);
		updateFriendGroup(friendId, group.getIdentifier());
		return group;
	}

	@Override
	public void deleteGroup(String id,String family,String friendId) throws AppBusinessException {
		friendDao.deleteGroup(id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("family", family);
		map.put("friend", friendId);
		friendDao.updateFriendGroupDef(map);
	}

	@Override
	public void updateFriendShield(boolean shield, String friendId)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("shield", shield);
		map.put("identifier", friendId);
		friendDao.updateFriend(map);
	}
	
	/**
	 * 查看俩家是否是好友
	 * @param myDoorplate		自己家的门牌号
	 * @param friendDoorplate	好友家的门牌号
	 * @return					0（不是好友）或1（是好友）
	 * @throws AppBusinessException
	 */
	public Integer getFriendByFamily(Map<String, Object> map)throws AppBusinessException{
		return friendDao.getFriendByFamily(map);
	}
	
}
